#!/usr/bin/env python3
import time

from db_helper import DbHelper
from http_helper import HttpHelper

db = DbHelper()
http = HttpHelper()

if __name__ == '__main__':
    act_list = http.fetch_top_activities(3, '')['records']
    valid_acts = []
    for act in act_list:
        # 并排显示在第三位的推广
        if act['type'] == 3:
            ids = []
            for a in act['data']:
                if not a['id'] in ids:
                    a['position'] = 3
                    valid_acts.append(a)
                    ids.append(a['id'])
        elif act['data']['isPromotion'] == 1:
            act['data']['position'] = 1
            valid_acts.append(act['data'])

    # # 目前列表中付费位置仅限1和3，其他数据不需要采集
    # for idx in [0, 2]:
    #     act = act_list[idx]
    #     # 第三条，可能并列显示的活动，过滤因第三条无人购买从而显示的正常活动
    #     if idx == 2 and act['type'] == 3:
    #         ids = []
    #         for a in act['data']:
    #             if not a['id'] in ids:
    #                 a['position'] = 3
    #                 valid_acts.append(a)
    #                 ids.append(a['id'])
    #     elif idx == 0:
    #         # 个人发起的活动没有俱乐部Id，不进行统计
    #         if act['data']['clubId']:
    #             act['data']['position'] = 1
    #             valid_acts.append(act['data'])

    for act in valid_acts:
        groups = http.fetch_activity_group_list(act['id'])
        new_member_count = 0
        male_count = 0
        female_count = 0
        total_level = 0
        for group in groups:
            for member in group['memberList']:
                total_level = total_level + member['level']
                # 统计男女数量
                if member['sex'] == 0:
                    female_count = female_count + 1
                else:
                    male_count = male_count + 1

                # 统计俱乐部新人数量
                if member['isFirstJoin'] == 1:
                    new_member_count = new_member_count + 1
        act['male_count'] = male_count
        act['female_count'] = female_count
        act['new_member_count'] = new_member_count
        if act['applyMemberNum'] > 0:
            act['level_average'] = total_level / act['applyMemberNum']
        else:
            act['level_average'] = 0

    db.save_top_activities(valid_acts)

    # 获取前两个活动的俱乐部的评价，供今后使用
    for idx in range(0, 2):
        if idx >= len(valid_acts):
            print('Finished')
            break
        end_loop = False
        page_no = 0
        while not end_loop:
            page_no = page_no + 1
            # print('xxxxx', len(valid_acts), idx)
            print('正在查询评论，当前页码：', page_no, valid_acts[idx]['clubId'])
            comments = http.fetch_club_comments(valid_acts[idx]['clubId'], page_no)['records']
            # print('comments:', comments)
            if len(comments) == 0:
                end_loop = True
                break

            for comment in comments:
                if db.find_comment(comment['id']) is None:
                    image = comment['pics'].split(',')[0] if comment['pics'] is not None else ''
                    db.create_comment(comment['id'], comment['content'], image)

            print('等待5秒继续下一次查询......')
            time.sleep(5)

        # act_detail = http.fetch_activity_detail(act['id'])
    # print('valid act:', valid_acts)